<template>

    <view class="publish-wrap bg-white">
        <!-- #ifdef APP  -->
        <cu-custom bgColor="bg-blue" :isBack="true">
            <block slot="backText">返回</block>
            <block slot="content">动态发布</block>
        </cu-custom>
        <!-- #endif -->
        <!-- #ifdef H5  -->
        <cu-custom bgColor="bg-blue" :isBack="true">
            <block slot="backText">返回</block>
            <block slot="content">动态发布</block>
        </cu-custom>
        <!-- #endif -->
        <!-- <u-navbar ref="navbar" :is-back="true" :back-text-style="{color: '#007aff'}" back-icon-color="#007aff"
            title-color="#007aff" back-text="返回" title="动态发布"></u-navbar> -->
        <view class="input-box padding-sm">
            <u-input type="textarea" :height="height" :auto-height="autoHeight" :border="border" placeholder="请输入..."
                v-model="content" />
            <!-- <te  xtarea class="area-top" placeholder="碎碎念..." v-model="content"></textarea> -->
        </view>
        <view class="cu-form-group img-box">
            <!-- <view class="next-title">上传</view> -->
            <view class="img-wrap margin-none">
                <easy-upload @delImage="dellistimg" @typesel="seltype" :dataList="imageList" :uploadUrl="$cfg.uploadurl"
                    :types="category" deleteUrl='http://localhost:3000/upload' :uploadCount="6"
                    @successImage="successImage" @successVideo="successvideo"></easy-upload>
                <!-- <view class="bg-img" v-for="(item,index) in imgList" :key="index">
                        <image :src="imgList[index]" mode="aspectFill" @tap="ViewImage(index)" class="img-item"></image>
                        <image src="@/static/img/publish/img_close.png" @tap.stop="DelImg" :data-index="index"
                            mode="widthFix" class="close-img"></image>
                    </view>
                    <view class="add-img" @tap="ChooseImage" v-if="imgList.length<9">
                        <text class='cuIcon-add link-color'></text>
                    </view> -->
            </view>
        </view>
        <view class="u-m-t-20">
            <u-cell-group>
                <u-cell-item icon="map" @click="f_map" :icon-style="aaa" :title="maptitle"
                    :class="maptitle!='位置'?'aa':''"></u-cell-item>
            </u-cell-group>
        </view>
        <view class="bottom-btn flex-center" @click="f_release">
            <view class="btn">发布</view>
        </view>
        <u-toast ref="uToast" />
    </view>
</template>

<script>
    import { mapMutations, mapState } from 'vuex';
    import {
        isnull
    } from '@/common/lib/fun.js'
    export default {
        data() {
            return {
                aaa: {
                    color: '#001f3f',
                },
                maptitle: '位置',
                imageList: [],
                category: 'image',
                height: 200,
                autoHeight: true,
                border: true,
                imgList: [],
                content: '',
                lat: '',
                lng: ''
            };
        },
        methods: {
            // setsfrelease
            ...mapMutations(['setsfrelease']),
            dellistimg(i) {
                this.imageList.splice(i, 1)
            },
            f_release() {
                var t = this
                var obj = {
                    title: t.content,
                    type: t.category,
                    attachment: JSON.stringify(t.imageList),
                    userDept: t.$cfg.user().deptId,
                    userId: t.$cfg.user().id,
                    userName: t.$cfg.user().name,
                    userId: t.$cfg.user().id,
                    userNum: t.$cfg.user().userNum,
                    address: t.maptitle,
                    lng: t.lng,
                    lat: t.lat,
                    userUrl: t.$cfg.user().userUrl,
                }
                if (t.maptitle == '位置') {
                    obj.address = ''
                }
                t.$api.req_post('staff_grow/staff/stDynamic/add', obj, 'json').then(res => {
                    if (res.success) {
                        this.$refs.uToast.show({
                            title: '发布成功',
                            type: 'success',
                            // url: '/pages/home/home',
                        })
                        uni.switchTab({
                            url: '/pages/home/home'
                        });
                        t.setsfrelease(true)
                    } else {
                        this.$refs.uToast.show({
                            title: '异常错误',
                            type: 'error'
                        })
                    }
                })
            },
            f_map() {
                var self = this
                uni.chooseLocation({
                    success: function (res) {
                        self.maptitle = res.name
                        self.lat = res.latitude
                        self.lng = res.longitude
                        console.log('位置名称：' + res.name);
                        console.log('详细地址：' + res.address);
                        console.log('纬度：' + res.latitude);
                        console.log('经度：' + res.longitude);
                    }
                });
            },
            seltype(a) {
                this.category = a
            },
            successImage(e) {
                var t = this
                // #ifdef MP-WEIXIN
                t.imageList.push(e)
                // #endif
                // uni.showModal({ 
                //     content: '上传成功,详细信息请看控制台'
                // })
            },
            successvideo(e) {
                var t = this
                // #ifdef MP-WEIXIN
                t.imageList = []
                t.imageList.push(e)
                // #endif
                // t.imageList.push(e)
                console.log(t.imageList);
            },
            // 选择图片
            ChooseImage() {
                let that = this
                uni.chooseImage({
                    count: 9, //默认9
                    sizeType: ['original'], //可以指定是原图还是压缩图，默认二者都有
                    sourceType: ['album'], //从相册选择
                    success: (res) => {
                        if (that.imgList.length != 0) {
                            that.imgList = that.imgList.concat(res.tempFilePaths)
                        } else {
                            that.imgList = res.tempFilePaths
                        }
                    }
                });
            },
            //显示删除弹窗
            DelImg(e) {
                let that = this
                that.imgList.splice(e.currentTarget.dataset.index, 1)
            },
            // 预览图片
            ViewImage(index) {
                let that = this
                uni.previewImage({
                    current: index,
                    urls: that.imgList
                })
            },
        }
    }
</script>

<style lang="less" scoped>
    .publish-wrap {
        .input-box {
            .area-top {
                width: 100%;
                box-shadow: inset 0 -1px 0 0 #ECECEC;
                min-height: 280rpx;
            }
        }

        .img-box {
            padding: 30rpx;
            display: flex;
            flex-direction: column;
            margin-top: 20rpx;
            margin-bottom: 160rpx;

            .next-title {
                width: 100%;
                font-size: 36rpx;
                color: var(--mainColor);
            }

            .font-red {
                color: #ff0000;
            }

            .img-wrap {
                width: 100%;
                font-size: 28rpx;
                line-height: 44rpx;
                color: #999999;
                margin: 20rpx 0;
            }

            .add-img {
                border: 4rpx dashed #999;
                font-size: 40rpx;
            }
        }

        .bottom-btn {
            position: fixed;
            bottom: 0;
            width: 100%;
            padding: 20rpx 0;

            .btn {
                border-radius: 40rpx;
                display: flex;
                align-items: center;
                justify-content: center;
                color: #FFFFFF;
                border: 1rpx solid var(--activeColor);
                background-color: var(--activeColor);
                box-shadow: 0 1px 10px 0px var(--activeColor);
                width: 400rpx;
                height: 80rpx;
            }
        }
    }

    .bg-img {
        overflow: visible !important;

        .img-item {
            border-radius: 20rpx;
        }

        .close-img {
            position: absolute;
            width: 36rpx !important;
            top: -18rpx;
            right: -18rpx;
        }
    }

    .aa {
        /* color: var(--activeColor); */
    }
</style>